package entity.file;

import util.JDBCUtil;

import java.io.File;
import java.io.FileWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @author ：mmzs
 * @date ：Created in 2020/9/14 11:19
 * @description：从本地磁盘读文件，返回装饰好的实例
 * @modified By：
 * @version: $
 */
public class FileDecorator {

    public AbstractFile doDecorator(AbstractFile file) {
        String text = file.loadTextFromFile();
        try {
            //text = text.replace("\n?", "\n");
            if (file instanceof PdfFile) {
                file.paperText = ((PdfFile) file).removeHeaderAndFooter(text);
                FileWriter fileWriter = new FileWriter(new File(file.path.replace("pdf", "txt")));
                fileWriter.write(text);
                fileWriter.close();
            }

            file.paperText = text;
            file.lineLength = file.countWordOneLine();

            String sql = "SELECT `title`,`authors`,`abstract`,`keywords`,`xmlfile`,`publish_year` FROM paper WHERE id = " + file.id;
            ResultSet resultSet = JDBCUtil.getConnection().createStatement().executeQuery(sql);
            resultSet.next();
            file.title = resultSet.getString(1);
            file.authors = resultSet.getString(2);
            file.paperAbstract = resultSet.getString(3);
            file.keywords = resultSet.getString(4);
            file.xmlFile = resultSet.getString(5);
            file.publish_year = resultSet.getString(6);

            if ("".equals(file.paperAbstract)) {
                file.extractPaperAbstract();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return file;
    }

    public static void main(String[] args) {
        FileDecorator fileDecorator = new FileDecorator();
        String location = "G:\\pdfpaper\\ai\\";
        Connection connection = JDBCUtil.getConnection();
        String sql = "SELECT paper.`id`, url FROM CHAIN, paper WHERE chain.`id` = paper.`id`";
        try {
            Statement statement = connection.createStatement();
            ResultSet resultSet1 = statement.executeQuery(sql);
            while (resultSet1.next()) {
                AbstractFile abstractFile = null;
                int id = resultSet1.getInt(1);
                String url = resultSet1.getString(2);
                String[] split = url.split("/");
                String fileName = split[split.length-1] + ".txt";
                String filePath = location + fileName;
                File file = new File(filePath);
                if(file.exists()) {
                    abstractFile = new TxtFile(id, file);
                    abstractFile = fileDecorator.doDecorator(abstractFile);
                    //传给下一层
                } else {
                    filePath = filePath.replace("txt", "pdf");
                    file = new File(filePath);
                    if (file.exists()) {
                        abstractFile = new PdfFile(id, file);
                        fileDecorator.doDecorator(abstractFile);
                    } else {
                        System.out.println(fileName);
                    }
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
}
